package com.insea.webapp.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.insea.model.Acte;
import com.insea.model.CategoriePat;
import com.insea.model.Examen;
import com.insea.model.Facture;
import com.insea.model.Patient;
import com.insea.model.User;

public class FactureAction extends BaseAction{

	private List patients;
	private List categories;
	private List examens;
	private List factures;
	private List actes;
	private String id;
	private Patient patient;
	private Facture facture;
	private Examen examen;
	private String categorie;
	private String query;
	
	
	public String list() {
		if(query == null || "".equals(query)){
			patients = patientManager.getAll();	
		}else{
			patients = patientManager.getPatientsByName(query);
		}
		return SUCCESS;
	}
	
	public List getPatients() {
		return patients;
	}
	
	public List getFactures(){
		return factures;
	}
	
	public List getActes() {
		return actes;
	}

	public List getCategories() {
		return categories;
	}

	public String listExamens() {
	    if (id != null) {
	        patient = patientManager.get(Long.valueOf(id));
	    } else {
	        patient = new Patient();
	    }
	    examens = examenManager.getListExamensByPatient(patient);
	    return SUCCESS;
	}
	
	public List getExamens() {
		return examens;
	}
	
	public String editFacture(){
		categories = categorieManager.getAll();
		if(id != null){
			examen = examenManager.get(Long.valueOf(id));
			getRequest().getSession().setAttribute("examen", examen);
			Float montant = 0f;
			Acte acte;
			for(int i=0; i<examen.getListActe().size(); i++){
				acte = examen.getListActe().get(i);
				montant = montant + Float.valueOf(acte.getTarifActe());
			}
			facture = new Facture();
			getRequest().getSession().setAttribute("montant", montant);
			facture.setMontant(montant.toString());
		}
		log.debug("examen : " + examen);
		return SUCCESS;
	}
	
	public String save(){
		if(facture.getNumFac() == null || "".equals(facture.getNumFac())){
			String key = "facutre.error";
			saveMessage(getText(key));
			facture = new Facture();
			Float montant = (Float) getRequest().getSession().getAttribute("montant");
			log.debug("montant from request : " + montant);
			facture.setMontant(montant.toString());
			categories = categorieManager.getAll();
			return ERROR;
		}
			examen = (Examen) getRequest().getSession().getAttribute("examen");
			log.debug("examen : " + examen);
			Float taux = categorieManager.getTauxByLibelle(categorie);
			Float montant = Float.valueOf(facture.getMontant());
			montant = montant * taux;
			facture.setMontant(montant.toString());
			Date date = new Date();
			facture.setDateFac(date);
			
			Authentication auth = SecurityContextHolder.getContext().getAuthentication();
			User user = (User) auth.getPrincipal();
			
			facture.setUser(user);
			factureManager.saveFacture(facture);
			examen.setFacture(facture);
			examen.setStatut(true);
			examen.setDateFinExamen(date);
			examenManager.save(examen);
			String key = "facture.added";
		    saveMessage(getText(key));
			return SUCCESS;
	}
	
	public String factureList(){
		if(query == null || "".equals(query)){
			factures = factureManager.getAll();
		}else{
			factures = factureManager.getFacturesByNum(query);
		}
		return SUCCESS;
	}
	
	public String viewFacture(){
		if(id!=null){
			facture = factureManager.get(Long.valueOf(id));
			examens = (List) examenManager.getExamenByFacture(facture);
			examen = (Examen) examens.get(0);
			patient = examen.getPatient();
			actes = examen.getListActe();
		}
		return SUCCESS;
	}
	
	// Getters and Setters
	
	

	public void setQuery(String query) {
		this.query = query;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public Facture getFacture() {
		return facture;
	}

	public void setFacture(Facture facture) {
		this.facture = facture;
	}

	public Examen getExamen() {
		return examen;
	}

	public void setExamen(Examen examen) {
		this.examen = examen;
	}

	public String getCategorie() {
		return categorie;
	}

	public void setCategorie(String categorie) {
		this.categorie = categorie;
	}



}
